#include "command.h"
#include "resources.h"

#include <iostream>

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>

using namespace std;
using namespace	boost;
using namespace	boost::filesystem;


void statistics_command::process_on_file(path p2file)
{
	string fp=p2file.string();
	LOG4CXX_DEBUG(logger,"process_on_file:[filename]"+	fp);		
	if(!is_regular_file(p2file))
	{
		LOG4CXX_ERROR(logger, "path is not regular file:"+fp);
		return ;
	}
		
	string parent_folder=p2file.branch_path().string();		
	string	leaf=p2file.leaf().string();
	long fs=file_size(p2file);
	LOG4CXX_INFO(logger, "file :"+fp+":"+	boost::lexical_cast<std::string>(fs)	+" bits"	
	+":parent_folder"+parent_folder+":leaf:"+leaf);	

	folder_info fi;
	bool folder_found = folder_info::find_resources_info(&fi,parent_folder);
	if(!folder_found)
	{
		LOG4CXX_INFO(logger, "FOLDER NOT FOUND IN RESOURCES, add into resource:"+parent_folder);									
		folder_info::insert_resources_info(&fi);
	}
	
	
}






void statistics_command::process_on_folder(path folder)
{
	string relative_path=folder.relative_path().native();
	string fp=folder.filename().native();
	string root_name=folder.root_name().native();
	LOG4CXX_DEBUG(logger,"PROCESSING FOLDER:[filename]"+fp);
	LOG4CXX_DEBUG(logger,"PROCESSING FOLDER	[relative_path]:"+relative_path);
	LOG4CXX_DEBUG(logger,"PROCESSING FOLDER:[root_name]:"+root_name);
	
	if(	!exists(folder) )
	{
		LOG4CXX_ERROR(logger,"PROCESSING FOLDER-NONT-EXISTS:"+fp);	
		return ;
	}
	
	LOG4CXX_DEBUG(logger,"PROCESSING FOLDER-EXISTS:"+fp);	
	
	bool isFile=is_regular_file(folder);	
	bool isDir=is_directory(folder);	
	LOG4CXX_DEBUG(logger,"isFile:"+	lexical_cast<std::string>(isFile));
	LOG4CXX_DEBUG(logger,"isDir:"+	lexical_cast<std::string>(isDir));

	if(isFile)
	{
		this->process_on_file(folder);
		return;
	}
	
	if(isDir)
	{
		//ensure folder exists in folder-table
		
		folder_info fi;
		bool folder_found = folder_info::find_resources_info(&fi,fp);
		if(!folder_found)
		{
			LOG4CXX_INFO(logger, "FOLDER NOT FOUND IN RESOURCES, add into resource:"+fp);									
			fi.setLeaf(fp);
			folder_info::insert_resources_info(&fi);
		}else
		{
		}
				
		//go inside this folder and execute on sub-folder/files						
		LOG4CXX_INFO(logger,"processing:"+fp);
		directory_iterator	itr(folder);		
		directory_iterator	dir_end;		
		
		for(;itr != dir_end; ++itr)
		{
			path c=itr->path();
			LOG4CXX_INFO(logger,c.filename());
			if(		is_regular_file(c) )			
			{
				process_on_file(c);
				continue;
			}
			
			if(is_directory(c) )
			{
				this->process_on_folder(c);																		
			}
			
			
			
		}
		
		
		return ;
	}
	
				
	LOG4CXX_ERROR(logger , "non-folder/yet non-file:"+fp);
	return ;
		
}



statistics_command::statistics_command()
{
	this->logger=log4cxx::Logger::getLogger("statistics_command");		
}


void	statistics_command::execute()
{
	LOG4CXX_INFO(this->logger,"begin to analyze directory:"+this->tree);		
	/**
	*iterate through the tree and import file information
	*/	
	this->execute(this->tree);	
}


void statistics_command::execute(string fp)
{
	path folder(fp);	
	this->process_on_folder(folder);
}

